package com.ricchieri.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.ricchieri.dao.interf.IPlaceDao;
import com.ricchieri.filter.PlaceFilter;
import com.ricchieri.model.Place;
import com.ricchieri.util.StringUtils;

public class PlaceDao extends GenericDao implements IPlaceDao {
	private static final long serialVersionUID = 9039754826744446962L;

	@Override
	@SuppressWarnings("unchecked")
	public List<Place> getAllPlaces(PlaceFilter placeFilter) {
		Criteria criteria = criteriaAllPlace(placeFilter);
		
		return criteria.list();
	}
	
	@Override
	public Place getPlaceById(PlaceFilter placeFilter) {
		Criteria criteria = criteriaAllPlace(placeFilter);
		
		return (Place) criteria.list().get(0);
	}
		
	private Criteria criteriaAllPlace(PlaceFilter placeFilter) {
		Criteria criteria = super.criteriaAllBasic(placeFilter, Place.class);
		
		if(!StringUtils.isEmpty(placeFilter.getDescription())) {
			criteria.add(Restrictions.eq(getTableName().concat(".description"), placeFilter.getDescription()));
		}

		return criteria;
	}

	@Override
	protected String getTableName() {
		return "place";
	}	
}
